//+--------------------------------------------------------------------------------
//| Copyright (c) 2012 - 2013 ### an.vuongngocduy
//| This file is a part of Compact Game Engine (CGE)
//+--------------------------------------------------------------------------------
#include "cgeRendererOGL.h"
#include "cgeCanvasWin32.h"

namespace cge
{
	namespace gfx
	{
		//=========================================================================================
		RendererOGL::RendererOGL()
		{
			mDevContext = 0;
			mGLContext = 0;
			mFixedPipeline = true;
		}

		//=========================================================================================
		RendererOGL::~RendererOGL()
		{
			wglMakeCurrent(0, 0);
			wglDeleteContext(mGLContext);
		}

		//=========================================================================================
		#ifdef CGE_PLATFORM_WINDOWS
		void RendererOGL::Initialize(Canvas* canvas)
		{
			mDevContext = GetDC(dynamic_cast<CanvasWin32*>(canvas)->GetHWND());

			PIXELFORMATDESCRIPTOR pfd = { 
				sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd  
				1,								// version number  
				PFD_DRAW_TO_WINDOW |			// support window  
				PFD_SUPPORT_OPENGL |			// support OpenGL  
				PFD_DOUBLEBUFFER,				// double buffered  
				PFD_TYPE_RGBA,					// RGBA type  
				24,								// 24-bit color depth  
				0, 0, 0, 0, 0, 0,				// color bits ignored  
				0,								// Set alpha buffer  
				0,								// shift bit ignored  
				0,								// set accumulation buffer  
				0, 0, 0, 0,						// accumulation bits ignored  
				32,								// 32-bit z-buffer  
				0,								// set stencil buffer  
				0,								// set auxiliary buffer  
				PFD_MAIN_PLANE,					// main layer  
				0,								// reserved  
				0, 0, 0							// layer masks ignored  
			}; 

			s32 pfdID = ChoosePixelFormat(mDevContext, &pfd);
			if(!pfdID) // failed to choose pixel format
				return;

			if(!SetPixelFormat(mDevContext, pfdID, &pfd)) // failed set pixel format
				return;

			mGLContext = wglCreateContext(mDevContext);
			if(!mGLContext) // failed to create GL context
				return;

			if(!wglMakeCurrent(mDevContext, mGLContext)) // failed to set GL rendering context
				return;

			glShadeModel(GL_SMOOTH);
			glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
		}
		#endif

		//=========================================================================================
		void RendererOGL::BeginScene()
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glLoadIdentity();
		}

		//=========================================================================================
		void RendererOGL::EndScene()
		{
			SwapBuffers(mDevContext);
		}

		//=========================================================================================
	}
}